using System.IO;


namespace Network 
{
    public class ProtoUtil
    {
        public static byte[] SerializeProto<T>(T instance)
        {
            byte[] buf = null;
            using (MemoryStream stream = new MemoryStream())
            {
                ProtoBuf.Serializer.Serialize<T>(stream, instance);
                buf = stream.ToArray();
            }
            return buf;
        }

        public static T DeserializeProto<T>(byte[] buf)
        {
            T instance;
            using (MemoryStream stream = new MemoryStream(buf))
            {
                instance = ProtoBuf.Serializer.Deserialize<T>(stream);
            }
            return instance;
        }

        public static object DeserializeProto(System.Type type, byte[] buf)
        {
            object instance;
            using (MemoryStream stream = new MemoryStream(buf))
            {
                instance = ProtoBuf.Serializer.Deserialize(type, stream);
            }
            return instance;
        }
    }
}
